package com.bhui.mytravel.controller;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.bhui.mytravel.response.ResponseData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


/**
 * @author JXS
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.dir}")
    private String dir;



    @GetMapping("/content")
    public ResponseEntity<byte[]> getFileContent(@RequestParam String filename) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //System.out.println(dir);
        try {
            // 获取OSS对象
            OSSObject ossObject = ossClient.getObject(bucketName, "file/config/"+filename);
            InputStream inputStream = ossObject.getObjectContent();

            // 读取文件内容为字节数组
            byte[] content = inputStream.readAllBytes();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream"); // 设置为二进制流
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\""); // 可选，设置文件名

            // 返回文件内容
            return ResponseEntity.ok()
                    .headers(headers)
                    .body(content);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }


    @PostMapping("/upload")
    public ResponseData<String> uploadFile(@RequestPart("file") MultipartFile file) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 获取文件名
            String originalFilename = file.getOriginalFilename();
            String uId = UUID.randomUUID().toString();
            String newFilename = uId + "_" + originalFilename;

            // 上传原文件到OSS
            ossClient.putObject(bucketName, dir + newFilename, file.getInputStream());

            // 返回文件访问路径
            String fileUrl = "https://" + bucketName + "." + endpoint + "/" + dir + newFilename;
            return ResponseData.succeed(fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseData.failed("File upload failed: " + e.getMessage());
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }
}
